/**
 * 资源受权管理初始化
 */
var PrincipalAuth = {
	principalType:null,
	principalId:null,
    backendResourceTree: null,
	frontResourceTree: null,
	apiResourceTree: null,
    roleTree:null,
    orgTree:null,
    layerIndex: -1,
    parentId:0
};

PrincipalAuth.onClickOrgTree = function (e, treeId, treeNode) {
	if(treeNode.id==0){
		Sys.info("根节点没有必要设置权限哦！")
		return;
	}
	PrincipalAuth.backendResourceTree.getObject().checkAllNodes(false);
	PrincipalAuth.frontResourceTree.getObject().checkAllNodes(false);
	PrincipalAuth.apiResourceTree.getObject().checkAllNodes(false);
    PrincipalAuth.principalId = treeNode.id;
    PrincipalAuth.principalType = treeNode.isParent?2:3;
    //load权限
    PrincipalAuth.loadPerms();
};

PrincipalAuth.onClickRoleTree = function (e, treeId, treeNode) {
	if(treeNode.id==0){
		Sys.info("根节点没有必要设置权限哦！")
		return;
	}
	PrincipalAuth.backendResourceTree.getObject().checkAllNodes(false);
	PrincipalAuth.frontResourceTree.getObject().checkAllNodes(false);
	PrincipalAuth.apiResourceTree.getObject().checkAllNodes(false);
    PrincipalAuth.principalId = treeNode.id;
    PrincipalAuth.principalType = 1;
    //load权限
    PrincipalAuth.loadPerms();
};

PrincipalAuth.loadPerms = function(){
	Sys.ajax(Sys.backendPath + "/permission/loadPermsByPrincipalId",{principalId:PrincipalAuth.principalId,principalType:PrincipalAuth.principalType}, function (res) {
        var backendResIds = res.data.backend;
        if(backendResIds){
        	for (var i = 0; i < backendResIds.length; i++) {
        	    var node = PrincipalAuth.backendResourceTree.getObject().getNodeByParam("id", backendResIds[i], null);
        	    if(node){
        	    	PrincipalAuth.backendResourceTree.getObject().checkNode(node, true, false);
        	    }
			}
        }
        var frontResIds = res.data.front;
        if(frontResIds){
        	for (var i = 0; i < frontResIds.length; i++) {
        	    var node = PrincipalAuth.frontResourceTree.getObject().getNodeByParam("id", frontResIds[i], null);
        	    if(node){
        	    	PrincipalAuth.frontResourceTree.getObject().checkNode(node, true, false);
        	    }
			}
        }
        var apiResIds = res.data.api;
        if(apiResIds){
        	for (var i = 0; i < apiResIds.length; i++) {
        	    var node = PrincipalAuth.apiResourceTree.getObject().getNodeByParam("id", apiResIds[i], null);
        	    if(node){
        	    	PrincipalAuth.apiResourceTree.getObject().checkNode(node, true, false);
        	    }
			}
        }
    });
}

PrincipalAuth.resourceTreeOnCheck = function (event, treeId, treeNode) {
	if((!(PrincipalAuth.principalType))||(!(PrincipalAuth.principalId))||PrincipalAuth.principalId==0){
		Sys.info("请先选择要设置的资源！")
		return;
	}
	var _data = {
		resourceId:treeNode.id,
		principalId:PrincipalAuth.principalId,
		principalType:PrincipalAuth.principalType,
		isAdd:treeNode.checked
	};
    //设置权限
    Sys.ajax(Sys.backendPath + "/permission/auth",_data, function (data) {
        Sys.info("权限设置成功!*^_^*");
    });
};

$(function () {
    var orgTree = new $ZTree("orgTree", Sys.backendPath + "/tree/user/admin");
    orgTree.bindOnClick(PrincipalAuth.onClickOrgTree);
    orgTree.init();
    PrincipalAuth.orgTree = orgTree;
    fuzzySearch('orgTree','#orgTreeSeachKey',null,true);
    
    var roleTree = new $ZTree("roleTree", Sys.backendPath + "/tree/role/everyone");
    roleTree.bindOnClick(PrincipalAuth.onClickRoleTree);
    roleTree.init();
    PrincipalAuth.roleTree = roleTree;
    fuzzySearch('roleTree','#roleTreeSeachKey',null,true);
    
    var backendResourceTreeSetting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "",
                "N": ""
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
    		onCheck: PrincipalAuth.resourceTreeOnCheck
    	}
    };
    var backendResourceTree = new $ZTree("backendResourceTree", Sys.backendPath + "/tree/resource/backend");
    backendResourceTree.setSettings(backendResourceTreeSetting);
    backendResourceTree.init();
    PrincipalAuth.backendResourceTree = backendResourceTree
    var backendRoot = backendResourceTree.getObject().getNodeByParam("id", 0, null);
    backendRoot.nocheck = true;
    backendResourceTree.getObject().updateNode(backendRoot);
    
    var frontResourceTreeSetting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "",
                "N": ""
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
    		onCheck: PrincipalAuth.resourceTreeOnCheck
    	}
    };
    var frontResourceTree = new $ZTree("frontResourceTree", Sys.backendPath + "/tree/resource/front");
    frontResourceTree.setSettings(frontResourceTreeSetting);
    frontResourceTree.init();
    PrincipalAuth.frontResourceTree = frontResourceTree
    var frontRoot = frontResourceTree.getObject().getNodeByParam("id", 0, null);
    frontRoot.nocheck = true;
    frontResourceTree.getObject().updateNode(frontRoot);
    
    var apiResourceTreeSetting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "",
                "N": ""
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
    		onCheck: PrincipalAuth.resourceTreeOnCheck
    	}
    };
    var apiResourceTree = new $ZTree("apiResourceTree", Sys.backendPath + "/tree/resource/api");
    apiResourceTree.setSettings(apiResourceTreeSetting);
    apiResourceTree.init();
    PrincipalAuth.apiResourceTree = apiResourceTree
    var apiRoot = apiResourceTree.getObject().getNodeByParam("id", 0, null);
    apiRoot.nocheck = true;
    apiResourceTree.getObject().updateNode(apiRoot);
    
});
